using System;
using System.Diagnostics;
using System.Globalization;

namespace Atomic.Libraries.Physics.Units
{
	[Serializable]
	[DebuggerDisplay("{DebuggerDisplay}")]
	public class Energy : IQuantity
	{
		private double value;

		private Energy(double value)
		{
			this.value = value;
		}

		public static Energy FromUnit(IUnitSystem system, double value)
		{
			return new Energy(value / system.EnergyScaling);
		}

		public static Energy FromElectronVolt(double value)
		{
			return FromUnit(UnitSystem.AtomicSystem, value);
		}

		public static Energy FromJoule(double value)
		{
			return FromUnit(UnitSystem.InternationalSystem, value);
		}
		
		public double ToUnit(IUnitSystem system)
		{
			return value * system.EnergyScaling;
		}

		public double ToElectronVolt()
		{
			return ToUnit(UnitSystem.AtomicSystem);
		}

		public double ToJoule()
		{
			return ToUnit(UnitSystem.InternationalSystem);
		}

		public static implicit operator double(Energy value)
		{
			return value.ToElectronVolt();
		}

		public static implicit operator Energy(double value)
		{
			return FromElectronVolt(value);
		}

		private string DebuggerDisplay
		{
			get
			{
				return string.Format(CultureInfo.InvariantCulture, "{0} eV = {1} J", ToElectronVolt(), ToJoule());
			}
		}
	}
}
